/**
 * Created by sun on 2016/8/4.
 */
define(function(require) {
	var app = require('/../js/app');
	require('ui-table');

	var toastr =require('toastr');

	app.useModule("ui.table");



	var datepicker = require('datepicker');
	app.controller('repay_listCtrl', ['$scope', '$http','$filter',
		function($scope, $http,$filter) {
			$scope.orderParams = {
				url: "/admin/invest/repayList"
			};
			datepicker($scope);
			$scope.ctrlScope=$scope;


			//查询订单
			$scope.openStatus3Modal= function (bidId) {
				$scope.add={};
				$scope.add.bidId=bidId;
				$scope.status3Modal = !$scope.status3Modal;
				$http({
					method: 'POST',
					url: "/admin/order/bidSearch",
					data:{bidId:bidId}
				}).success(function(data) {
					if(data.respSubCode=='000000'){
						if(data.bidStatus == 'W'){
							$scope.add.respSubdes='等待中';
							$scope.add.status='W';
						}else if(data.bidStatus == 'II'){
							$scope.add.respSubdes='投资中';
							$scope.add.status='II';
						}else if(data.bidStatus == 'GI'){
							$scope.add.respSubdes='放款中';
							$scope.add.status='GI';
						}else if(data.bidStatus == 'GD'){
							$scope.add.respSubdes='已放款';
							$scope.add.status='S';
							$scope.repayStatus= function () {
								$http({
									method: 'POST',
									url: "/pay/notice/bidRepay",
									data:$scope.add
								}).success(function(data) {
									$scope.add={};
									if(data.result=='success'){
										toastr.success('补单成功');
										$scope.status3Modal = !$scope.status3Modal;
									}else{
										toastr.error('补单失败');
										$scope.status3Modal = !$scope.status3Modal;
									}

								}).error(function(data) {});
							}
						}else if(data.bidStatus == 'CI'){
							$scope.add.respSubdes='流标中';
							$scope.add.status='CI';
						}else if(data.bidStatus == 'CD'){
							$scope.add.respSubdes='已流标';
							$scope.add.status='CD';
						}
					}else if(data.respCode=='200103'){
						$scope.add.respSubdes='商户流水已存在'
						$scope.add.status='F';
					}else if(data.respCode=='100000'){
						$scope.add.respSubdes='参数校验失败'
						$scope.add.status='F';
					}else if(data.respCode=='200004'){
						$scope.add.respSubdes='标的不存在';
						$scope.add.status='F';
					}else{
						$scope.add.respSubdes='服务器繁忙'
						$scope.add.status='F';
					}



				}).error(function(data) {});
			};

			//创建
			$scope.addOrderModal = function() {
				$scope.orderModal = !$scope.orderModal;
			};
			$scope.getShow=function(parm){
				$http({
					method: 'POST',
					url: "/admin/invest/statRepay",
					data:parm,
				}).success(function(data) {
					console.info(data);
					$scope.show=data;
				}).error(function(data) {});
			}
			$scope.getShow();

			//全局查询
			$scope.search = function() {
				$scope.ctrlScope.itemsearch.repayStartTime=$scope.ctrlScope.itemsearch.repayStartTimex;
				$scope.ctrlScope.itemsearch.repayEndTime=$scope.ctrlScope.itemsearch.repayEndTimex;
				$scope.ctrlScope.itemsearch.actualStartTime=$scope.ctrlScope.itemsearch.actualStartTimex;
				$scope.ctrlScope.itemsearch.actualEndTime=$scope.ctrlScope.itemsearch.actualEndTimex;
				$scope.ctrlScope.itemsearch.repayStartTime=$filter('date')($scope.ctrlScope.itemsearch.repayStartTime,'yyyy-MM-dd');
				$scope.ctrlScope.itemsearch.repayEndTime=$filter('date')($scope.ctrlScope.itemsearch.repayEndTime,'yyyy-MM-dd');
				$scope.ctrlScope.itemsearch.actualStartTime=$filter('date')($scope.ctrlScope.itemsearch.actualStartTime,'yyyy-MM-dd');
				$scope.ctrlScope.itemsearch.actualEndTime=$filter('date')($scope.ctrlScope.itemsearch.actualEndTime,'yyyy-MM-dd');
				//表格查询参数设置
				$scope.orderParams.ajaxParam=$scope.ctrlScope.itemsearch;
				//表格刷新
				$scope.orderParams.reload();
				var parm=$scope.ctrlScope.itemsearch;
				$scope.getShow(parm);
			};
			//全局查询重置
			$scope.reset=function(){
				$scope.orderParams.ajaxParam={};
				$scope.ctrlScope.itemsearch={};
				$scope.orderParams.reload();
				$scope.getShow();
			};

			$scope.contractParms = {
				url: "/admin/invest/listByItemId",

			};
			//合同
			$scope.checkContractModal= function (itemId) {
				$scope.contractModal=!$scope.contractModal;
				$scope.contractParms.ajaxParam={itemId:itemId};
				$scope.contractParms.reload();
			};

			//合同
			$scope.generateContractModal= function (itemId) {
				toastr.success('处理中');
				var parm = {
					"itemId": itemId,
				}
				$http({
					method: 'POST',
					url: "/admin/invest/contractGenerate",
					data: parm,
				}).success(function(data) {

				}).error(function(data) {});
			};

			//合同下载
			$scope.openURL= function (url) {
				
				window.open(url)
			};

			//确认是否还款
			$scope.confirmBtn = function(itemId, repayCurrentPeriod,repayType){
				$scope.confirmModal = !$scope.confirmModal;
				$scope.confirmObject = {
					"itemId": itemId,
					"currentPeriod" : repayCurrentPeriod,
					"repayType":repayType
				};

			};
			$scope.closeBtn = function(){
				$scope.confirmModal = !$scope.confirmModal;
			};


			//还款
			$scope.modifyModal = function() {
				var url='';
				if($scope.confirmObject.repayType==1){
					url='/admin/invest/itemRepay';
				} else if($scope.confirmObject.repayType==2){
					url='/admin/invest/borrowerRepay';
				}
				var parm = {
					"itemId": $scope.confirmObject.itemId,
					"currentPeriod" : $scope.confirmObject.currentPeriod
				};
				$http({
					method: 'POST',
					url: url,
					data: parm
				}).success(function(data) {
						//console.log(data);
						if(data.result=='fail'){
							if(data.messageText){
								toastr.error(data.messageText)
							}else{
								toastr.error('还款失败')
							}

						}else{
							toastr.success('还款成功');
							$scope.orderParams.reload();
							$scope.getShow();
						}

				}).error(function(data){

				});

			};
			//导出
			$scope.download=function(){
				var params=$scope.ctrlScope.itemsearch;
				$http.post("/admin/invest/excel/repay",params)
					.success(function (data){
						console.info(data);
						if(data.result=="success"){
							window.open(data.file);
						}else{
							toastr.error("导出失败");
						}
					}).error(function(data){

					});
			};


			//删除
			$scope.deleteContract = function(contractId) {
				var parm = {
					"contractId": contractId,
				}
				if (confirm("确认删除?")) {
					$http({
						method: 'POST',
						url: "/admin/invest/deleteContract",
						data: parm,
					}).success(function(data) {
						console.log(data)
						if(data.result=='fail'){
							if(data.messageText){
								toastr.error(data.messageText)
							}else{
								toastr.error('删除失败')
							}

						}else{
							toastr.success('删除成功');
							$scope.orderParams.reload();
							$scope.getShow();
						}

					}).error(function(data) {});
				} else {
					return
				}
			};
		}
	]);


});